var menu = function () {
    return {
        initMenu:function () {
            loadmenu();
        },
        menuClick:function () {
            menuClienk();
        },
        checkSessionExpire:function () {
            sessionIsNotExpired();
        }
    };



    function loadmenu() {
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            url: "/sysPermission/menu",
            async: false,
            type: "get",
            success: function (data) {
                var arrs = data.data;
                if (data.code == 200) {
                    var menu = menuFn(0,arrs);
                    var menulist = $(".page-sidebar-menu");
                    menulist.append(menu);
                }
            }
        })
    }
    function menuFn(id,data) {
        var html = "";
        if (data.length == 0) {
            return html;
        }
        $.each(data, function(idx, obj) {
            html += appendFirst(idx, obj);
            if (obj.childrenList.length == 0) {
                return;
            }
            html += "<ul class='sub-menu'>";
            $.each(obj.childrenList, function (idxc, objc){
                html += appendSecond(idxc, objc);
                if (objc.childrenList.length == 0) {
                    return;
                }
                html += "<ul class='sub-menu'>";
                $.each(objc.childrenList, function (inxt, objt) {
                    html += appendThread(inxt, objt);
                });
                html += "</ul>";
            })
            html += "</ul></li>";
        });
        return html;
    }

    function appendFirst(idx, obj) {
        var firstHtml = "";
        if (idx == 0) {
            firstHtml = "class='active '"
        }
        var desc = getDesc(obj);
        var html = "<li "
            + firstHtml
            + "><a  url='"+obj.url+"'><i class='icon-folder-open'></i> <span class='title'>"
            + obj.name
            + desc;
        return html;
    }
    function getDesc(obj) {
        var desc = "";
        if (obj.childrenList.length > 0) {
            desc = "</span><span class='arrow '></span></a>";
        }
        return desc;
    }

    function appendSecond(idx, obj) {
        var desc = getDesc(obj);
        var html = "<li><a url='"+obj.url+"'><i class='icon-globe'></i>"
            + obj.name
            + desc;
        return html;
    }

    function appendThread(idx, obj) {
        var html = "<li><a  url='"+obj.url+"'><i class='icon-user'></i>"
            + obj.name
            + "</a></li>";
        return html;
    }

    function menuClienk () {
        $('.page-sidebar-menu li').on('click', function(event) {
            var url = $(this).find("a").attr("url");
            if ("null"!=url) {
                $(".maincontext").load(url);
            }
            event.stopPropagation();
        })
    }


    function sessionIsNotExpired() {
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            url: 'redis/session',
            async: false,
            type: "get",
            success: function (data) {
               console.log(data);
                 if (data.code != 200) {
                    window.location.href = "/";
                 }
            }
        })
    }

    function removeClass() {
        $('.page-sidebar-menu li').each(function(){
            $(this).attr("class", "");
        });
    }

function ajaxLoadData(url) {
    $.ajax({
        contentType: "application/json;charset=UTF-8",
        url: url,
        async: false,
        type: "get",
        success: function () {
            /*var arrs = data.data.records;
            console.log(arrs);*/
            $(".maincontext").load(url);
           /* if (data.code == 200) {
                var datas = convertData(arrs);
                $(".maincontext").load(html, function(){
                    var tab = $(".datalist");
                    tab.append(datas);
                });
            }*/
        }
    })
}
//用户list模板
function convertData(arrs) {
    var arrData = '';
    if (arrs.length == 0) {
        return html;
    }
    $.each(arrs, function (index, o) {
        arrData += "<tr class="+index+"><td>" + o.name + "</td><td>" + o.phone + "</td>" +
            "<td>" + o.email + "</td>" +
            "<td><a href='#' class='btn mini purple'>" +
            "<i class='icon-edit'></i> 编辑</a>" +
            "<a href='#' class='btn mini black'><i class='icon-trash'></i>删除</a></td></tr>";
    });

    return arrData;
}

}();


